package com.gxar.file.server.base.config;

import com.gxar.file.server.base.common.properties.RedisProperties;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Redis configuration.
 *
 * @author linsy
 */
@Configuration
@RequiredArgsConstructor
public class RedisConfig {

    private final RedisProperties redisProperties;

    /**
     * @return
     */
    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisProperties.getJedisProperties().getMaxIdle());
        jedisPoolConfig.setMinIdle(redisProperties.getJedisProperties().getMinIdle());
        jedisPoolConfig.setMaxTotal(redisProperties.getJedisProperties().getMaxActive());
        if (StringUtils.isNotBlank(redisProperties.getPassword())) {
            return new JedisPool(jedisPoolConfig, redisProperties.getHost(), redisProperties.getPort(),
                    redisProperties.getTimeout(), redisProperties.getUsername(), redisProperties.getPassword());
        }
        return new JedisPool(jedisPoolConfig, redisProperties.getHost(), redisProperties.getPort(),
                redisProperties.getTimeout());
    }


}